package itheima03;

import utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/*
    preparedStatement：预编译对象，是Statement对象的子类。

    特点：
        - 性能高
        - 会把sql语句先编译
        - 能过滤掉用户输入的关键字。因为用户输入的任何内容,都会做原样拼接,不会出现sql注入攻击

    删除所有记录
 */
/*public class Demo05PreparedStatement {
    public static void main(String[] args) throws Exception {
        Connection con = JDBCUtil.getConnection();
        String sql = "delete from users where uid=?";
        PreparedStatement pstmt = con.prepareStatement(sql);
        //给?赋值,并执行sql
        for (int i = 1; i <= 5; i++) {
            pstmt.setString(1,"u00"+i);
            int result = pstmt.executeUpdate();
            //处理结果
            if (result > 0) {
                System.out.println("删除第"+i+"条记录成功~~~");
            } else {
                System.out.println("删除第"+i+"条记录失败~~~");
            }
        }
        JDBCUtil.release(con,pstmt,null);
    }
}*/

public class Demo05PreparedStatement {
    public static void main(String[] args) throws SQLException {
        Connection connection=JDBCUtil.getConnection();
        String sql="delete from users where uid=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i=0;i<5;i++){
            preparedStatement.setObject(1,"u00"+i);
            int aaa=preparedStatement.executeUpdate();
            if(aaa>0){
                System.out.println("删除第"+i+"条数据成功");
            }else {
                System.out.println("删除第"+i+"条数据失败");
            }
        }
        JDBCUtil.release(connection,preparedStatement,null);
    }
}